package com.wangjw.springbootdemo.controller;

import com.wangjw.springbootdemo.pojo.IMoocJSONResult;
import com.wangjw.springbootdemo.pojo.SysUser;
import com.wangjw.springbootdemo.service.IUserService;
import com.wangjw.springbootdemo.utils.JsonUtils;
import com.wangjw.springbootdemo.utils.RedisOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by wangjw on 18/5/30.
 */
@RestController
@RequestMapping("redis")
public class RedisController {

    @Autowired
    private StringRedisTemplate strRedis;

    @Autowired
    private RedisOperator redisOperator;

    @Autowired
    private IUserService iUserService;

    @RequestMapping("/test")
    public IMoocJSONResult test() {
        strRedis.opsForValue().set("wangjw", "王建文");
        return IMoocJSONResult.ok(strRedis.opsForValue().get("wangjw"));
    }

    @RequestMapping("/testObj")
    public IMoocJSONResult testObj() {
        SysUser user = iUserService.queryUserById("180529H3F5K4ZSCH");
        strRedis.opsForValue().set("json:user", JsonUtils.objectToJson(user));

        SysUser retUser = JsonUtils.jsonToPojo(strRedis.opsForValue().get("json:user"), SysUser.class);
        return IMoocJSONResult.ok(retUser);
    }

    @RequestMapping("/testObj2")
    public IMoocJSONResult testObj2() {
        SysUser user = iUserService.queryUserById("180529H3F5K4ZSCH");
        redisOperator.set("json:user1", JsonUtils.objectToJson(user));

        SysUser retUser = JsonUtils.jsonToPojo(redisOperator.get("json:user1"), SysUser.class);
        return IMoocJSONResult.ok(retUser);
    }
}
